package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.DepartmentMapper;
import com.hrma.entity.Department;
import com.hrma.entity.DepartmentDTO;
import com.hrma.entity.SelectResult;
import com.hrma.entity.exception.ContentExistException;
import com.hrma.services.DepartmentService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentImpl implements DepartmentService {
    private final DepartmentMapper departmentMapper;

    public DepartmentImpl(DepartmentMapper departmentMapper) {
        this.departmentMapper = departmentMapper;
    }


    @Override
    public Department addDepartment(Department data) {
        if (departmentMapper.exist(null, data.getDepartmentName(), null)) {
            throw new ContentExistException("该部门已存在！");
        }
        departmentMapper.addDepartment(data);
        return data;
    }

    @Override
    public void deleteDepartment(int id) {
        if (!departmentMapper.exist(id, null, null)) {
            throw new RuntimeException("id字段不存在！");
        }
        departmentMapper.deleteDepartment(id);
    }

    @Override
    public void updateDepartment(Department data) {
        if (!departmentMapper.exist(data.getId(), null, null)) {
            throw new RuntimeException("id字段不存在！");
        }
        departmentMapper.updateDepartment(data);
    }

    @Override
    public List<SelectResult> selectDepartment() {
        return departmentMapper.selectDepartment();
    }

    @Override
    public Page<Department> getList(DepartmentDTO data) {
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return departmentMapper.getList(data);
    }
}
